package yagwl.server.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import yagwl.service.fnd.flex.ConfigService;

public class RestApiTest {

  private static final String urlBase = "http://127.0.0.1:8888/api";
  private static String sessionId;

  public static void main(String[] args) {
    try {
      testNoAuth();
      testCatalog();
      testAddToCart();
      testRemoveFromCart();
      testCart();
      testLoginUnAuthenticated();
      testLogin();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  private static void testNoAuth() throws IOException {
    URL url = new URL(urlBase + "?api=catalog");
    URLConnection conn = url.openConnection();
    InputStream is = null;
    try {
      is = conn.getInputStream();
    } catch (IOException e) {
      e.printStackTrace();
    }
    printInputStream(conn, is);
  }

  private static void testCatalog() throws IOException {
    doHttpCall(urlBase + "?api=catalog");
  }

  private static void testAddToCart() throws IOException {
    doHttpCall(urlBase + "?api=addToCart&productId=5034");
    doHttpCall(urlBase + "?api=addToCart&productId=5034");
    doHttpCall(urlBase + "?api=addToCart&productId=5036");
  }

  private static void testRemoveFromCart() throws IOException {
    doHttpCall(urlBase + "?api=removeFromCart&productId=5036");
  }

  private static void testCart() throws IOException {
    doHttpCall(urlBase + "?api=cart");
  }

  private static void testLoginUnAuthenticated() throws IOException {
    doHttpCall(urlBase + "?api=login");
  }

  private static void testLogin() throws IOException {
    doHttpCall(urlBase + "?api=login&username=username&password=password");
  }

  private static void doHttpCall(String urlStr) throws IOException {
    URL url = new URL(urlStr);
    URLConnection conn = url.openConnection();
    conn.setRequestProperty("Authorization", "MTEP <SenderId>");
    String cookieName = ConfigService.getInstance()
        .getSessionCookieName();
    if (sessionId != null)
      conn.setRequestProperty("Cookie", cookieName + "=" + sessionId);
    try {
      InputStream is = conn.getInputStream();
      printInputStream(conn, is);
    } catch (IOException e) {
    }
    // if session cookie was sent in the response, hold the session id
    String cookie = conn.getHeaderField("Set-Cookie");
    if (cookie != null) {
      String name = cookie.substring(0, cookie.indexOf("="));
      if (!name.equals(cookieName))
        return;
      String value = cookie.indexOf(";") < 0 ? cookie.substring(cookie
          .indexOf("=") + 1) : cookie.substring(cookie.indexOf("=") + 1, cookie
          .indexOf(";"));
      sessionId = value;
      //System.out.println("Session id at end: " + sessionId);
    }
  }

  private static void printInputStream(URLConnection conn, InputStream is)
      throws IOException {
    try {
      Thread.sleep(1000L);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("===========================================");
    System.out.println("Session id at beginning: " + sessionId);
    Map<String, List<String>> headers = conn.getHeaderFields();
    for (String name : headers.keySet()) {
      if (name == null)
        System.out.println(conn.getHeaderField(name));
      else
      System.out.println(name + ": " + conn.getHeaderField(name));
    }
    if (is == null)
      return;
    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
      System.out.println(inputLine);
    in.close();
  }

}
